# Main Rules
# ==========
#
# make [all]: does a sandboxed install of trellys X.
#             Assumes you have Cabal installed, and that the default
#             Cabal install dir is on your path (~/.cabal/bin for
#             default "per-user" setup).  A trellys binary will be
#             installed in ./cabal-dev/bin/trellys, and symlinked
#             to test/trellys (the only place you're expected to run
#             `trellys`).
#
# make test:  runs all tests in ./test.
#
# make sandbox-trellys:
#             does a sandboxed install of trellys X.  Quicker than
#             `make all`. Assumes you ran `make all` previously.
#
# make install: does a `cabal install` of trellys X.
#
# make ghci, make cabal-dev-ghci: start ghci with appropriate includes.

.PHONY: all sandbox-install sandbox-uninstall sandbox-trellys \
	    install uninstall clean test ghci cabal-dev-ghci

all: sandbox-install

sandbox-install: sandbox-trellys

sandbox-uninstall:
	-rm -rf cabal-dev

sandbox-trellys: cabal-dev
	cabal-dev install .
	ln -fs `pwd`/cabal-dev/bin/nax test

# You need to have the cabal install dir on your path (by default
# ~/.cabal/bin) so that `cabal-dev` command is found.
cabal-dev:
	cabal install cabal-dev

install:
#	cabal install
	cd src && cabal install

uninstall:
	-ghc-pkg unregister `ghc-pkg list | grep trellys`
	@echo
	@echo You need to manually delete any trellys binaries on your path.
	@echo You can find them with \`which trellys\`

clean:
	-rm -rf dist tmp test/nax .hpc
	find ./ -name '*.tix' -exec rm {} +

test:
	cd test && make

etags:
	find src ../lib/layout-token/src -name .svn -prune \
	  -o -name '*.hs' -print | xargs hasktags --etags

# Start ghci through cabal-dev.
#
# This kind of works, but I'm not sure how to load the Trellys (main)
# module here and it's not available from `:m + Trellys` in the ghci
# started.
cabal-dev-ghci:
	cd src && cabal-dev ghci

ghc_include = src:../lib/layout-token/src
# Start ghci with appropriate include dirs.
ghci:
	ghci -i$(ghc_include) src/Main.hs

# Compile w/o cabal: much quicker than cabal-dev
test/nax: src/*.hs
    # Save the intermediate .o and .hi files in a separate dir (./tmp)
    # so that location information in inferior ghci in emacs will work
    # (C-c M-.).
    #
    # E.g. do `ghc_flags=-fwarn-unused-binds make test/nax` to get
    # warnings.
	ghc $(ghc_flags) --make src/Main.hs -i$(ghc_include) \
	  -o test/nax -hidir tmp -odir tmp

test/nax.whiney:
	make clean
	ghc_flags="-fwarn-unused-binds -fwarn-unused-imports" \
	  make test/nax

.PHONEY: hpc build-hpc run-hpc
hpc: clean build-hpc run-hpc
build-hpc:
	ghc_flags="-fhpc" \
	  make test/nax
run-hpc:
# There may be an easier way to make all the hpc files end up in .hpc
# ...
	mkdir -p .hpc
	cd .hpc && ../test/nax ../test/data.nax < /dev/null > /dev/null
	hpc report .hpc/*.tix
	hpc markup .hpc/*.tix --destdir .hpc
